<script setup>
import PageContainer from '@/components/PageContainer.vue';
import { ref } from 'vue'
import { updateUserPasswordService } from '@/api/user'
const userInfo = ref({ old_pwd: '', new_pwd: '', re_pwd: '' })
const rules = {
  old_pwd: [
    { required: true, message: '请输入旧密码', trigger: 'blur' }
  ],
  new_pwd: [
    { required: true, message: '请输入新密码', trigger: 'blur' },
    {
      pattern: /^\S{6,12}$/,
      message: '密码必须是6-12位的非空字符串',
      trigger: 'blur'
    }
  ],
  re_pwd: [
    { required: true, message: '请确认新密码', trigger: 'blur' },
    {
      validator: (val) => val.number === userInfo.value.new_pwd.number,
      message: '两次输入的新密码不一致',
      trigger: 'blur'
    }
  ]
}
const formRef = ref(null)
//提交修改
const onSubmit = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交修改
  const res = await updateUserPasswordService(userInfo.value)
  console.log(res)
  if (res.status === 200) {
    ElMessage.success('修改成功')
    window.location.reload()
  }
}
// 重置表单
const resetForm = () => {
  userInfo.value = { old_pwd: '', new_pwd: '', re_pwd: '' }
  formRef.value.resetFields()
}
</script>
<template>
  <PageContainer title="重置密码">
    <el-row>
      <el-col :span="12">
        <el-form
          :model="userInfo"
          :rules="rules"
          ref="formRef"
          label-width="100px"
          size="large"
        >
          <el-form-item label="原密码" prop="old_pwd">
            <el-input v-model="userInfo.old_pwd" type="password"></el-input>
          </el-form-item>
          <el-form-item label="新密码" prop="new_pwd">
            <el-input v-model="userInfo.new_pwd" type="password"></el-input>
          </el-form-item>
          <el-form-item label="确认新密码" prop="re_pwd">
            <el-input v-model="userInfo.re_pwd" type="password"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="onSubmit">提交修改</el-button>
            <el-button type="info" @click="resetForm">重置</el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </PageContainer>
</template>
